var prefix = "/yw/appraisalApply";
var prefix_O = "/yw/appraisal";
var prefix_1 = "/yw/appraisalUser";
$(document).ready(function () {
    validateRule();
    selectUser();
    selectLoadOne();
});

$.validator.setDefaults({
    submitHandler: function () {
        save();
    }
});

function save() {
    if (check()) {
        layer.msg("请输入所有必填信息");
        return;
    }
    var amount = $('#amount').val();
    if (amount <= 0) {
        layer.msg("指标数量不能小于1");
        return;
    }
    $.ajax({
        cache: true,
        type: "POST",
        url: prefix + "/apply",
        data: $('#applyForm').serialize(), // 你的formid
        async: false,
        error: function () {
            parent.layer.alert("网络超时");
        },
        success: function (data) {
            if (data.code === 0) {
                parent.layer.msg("操作成功");
                parent.reLoad();
                var index = parent.layer.getFrameIndex(window.name);
                parent.layer.close(index);

            } else {
                parent.layer.alert(data.msg)
            }
        }
    });
}

function validateRule() {
    var icon = "<i class='fa fa-times-circle'></i> ";
    $("#applyForm").validate({
        rules: {
            userId: {
                required: true
            }
            , typeId: {
                required: true
            }
        },
        messages: {
            userId: {
                required: icon + "请输入名字"
            }
            , typeId: {
                required: icon + "请输入名字"
            }
        }
    })
}

function selectUser() {
    var html = "";
    $.ajax({
        url: prefix_1 + '/list/appraisalUser',
        success: function (data) {
            //加载数据
            for (var i = 0; i < data.length; i++) {
                html += '<option value="' + data[i].userId + '">' + data[i].name + '</option>'
            }
            $("#userId").append(html);
            $("#userId").chosen({
                maxHeight: 200
            });
        }
    });
}

function selectLoadOne() {
    var html = "";
    $.ajax({
        url: prefix_O + '/type',
        success: function (data) {
            //加载数据
            for (var i = 0; i < data.length; i++) {
                html += '<option value="' + data[i].appraisalValue + '">' + data[i].appraisalName + '</option>'
            }
            $("#typeId").append(html);
            $("#typeId").chosen({
                maxHeight: 200
            });
        }
    });
}

//点击事件
$('#userId').on('change', function (e, params) {
    console.log(params.selected);
    $('#userName').val($('#userId').find('option:selected').text());
});

//点击事件
$('#typeId').on('change', function () {
    $('#typeName').val($('#typeId').find('option:selected').text());
    $('#itemOneId').val("");
    $('#itemOneName').val("");
    $('#itemName').val("");
    $('#itemTwoId').val("");
    $('#itemTwoName').val("");

});

function check() {
    return !($('#userId').val() && $('#itemOneId').val() && $('#itemName').val());
}

function openAppraisal() {
    var type = $('#typeId').val();
    if (type) {
        layer.open({
            type: 2,
            title: "指标选择",
            area: ['800px', '500px'],
            content: prefix_O + "/appraisalTree?type=" + type
        })
    } else {
        layer.msg("请先选择一级指标");
    }
}

function loadAppraisalOne(itemOneId, itemOneName, itemName) {
    $("#itemOneId").val(itemOneId);
    $("#itemOneName").val(itemOneName);
    $("#itemName").val(itemName);
}

function loadAppraisalTwo(itemTwoId, itemTwoName) {
    $("#itemTwoId").val(itemTwoId);
    $("#itemTwoName").val(itemTwoName);
}

function addApplyData() {
    layer.open({
        type: 2,
        title: "指标选择",
        area: ['800px', '500px'],
        content: prefix + "/applyDataAdd"
    })

}

function dataId(dataId) {
    $('#dataId').val(dataId);
}